package va.radiomaria.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.JPanel;

import va.radiomaria.audio.Mood;

public class MoodPanel extends JPanel implements MouseListener {

	private static final long serialVersionUID = 1L;

	private HashMap<Mood, MoodIcon> moodIcons = new HashMap<Mood, MoodIcon>();
	
	private ActionListener controller = null;
	
	private Mood currentMood = Mood.PARTY;
	
	public MoodPanel(ActionListener controller) {
		this.controller = controller;
		MoodIcon partyIcon = new MoodIcon("Icons/Disco1.png", "Icons/Disco2.png", Mood.PARTY, this);
		partyIcon.setIsActive(true);
		addMoodIcon(partyIcon);
		addMoodIcon(new MoodIcon("Icons/heart1.png", "Icons/heart2.png", Mood.ROMANTIC, this));
		addMoodIcon(new MoodIcon("Icons/Workout1.png", "Icons/Workout2.png", Mood.WORKOUT, this));
	}
	
	public void sendMoodChangedAction(Mood mood) {
		currentMood = mood;
		controller.actionPerformed(new ActionEvent(this, -1, CommonActionCommand.MOOD_CHANGED.toString()));
		updateImages();
	}
	
	public Mood getCurrentMood() {
		return currentMood;
	}
	
	public void updateImages() {
		for(MoodIcon icon: moodIcons.values()) {
			if(icon.getMood() ==currentMood) {
				icon.setIsActive(true);
			}
			else {
				icon.setIsActive(false);
			}
		}
	}

	void addMoodIcon(MoodIcon moodIcon) {
		moodIcons.put(moodIcon.getMood(), moodIcon);
		this.add(moodIcon);
	}

	@Override
	public void mouseClicked(MouseEvent mouseEvent) {
		try{
			MoodIcon source = (MoodIcon) mouseEvent.getSource();
			sendMoodChangedAction(source.getMood());
		}
		catch (Exception e) {
			System.err.println("could not cast source " + mouseEvent.getSource().toString());
		}
		
	}

	/*
	 * Unused MouseListener Events
	 */
	
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
